package informationGet.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.whu.zj.MyApplication;
import com.whu.zj.R;
import com.whu.zj.UIHandler;

import java.util.ArrayList;
import java.util.List;

import emergency.dto.Alert;
import informationGet.activity.ShowImage;
import informationGet.activity.TakeInformationActivity;
import informationGet.dto.Points;

import static com.whu.zj.R.layout.item_pointsshow;

/**
 * Created by Administrator on 2016/3/16.
 */
public class ShowPointsAdapter extends BaseAdapter {

    List<Points> pointsList = new ArrayList<Points>();

    public void setPointsList(List<Points> pointsList) {
        this.pointsList = pointsList;
    }

    @Override
    public int getCount() {
        return pointsList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null)
        {
            viewHolder = new ViewHolder();
            convertView = View.inflate(MyApplication.share(), R.layout.item_pointsshow,null);
            viewHolder.tv_message = (TextView) convertView.findViewById(R.id.id_tv_content);
            viewHolder.tv_time = (TextView) convertView.findViewById(R.id.id_tv_time);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.id_img_photo);
            viewHolder.tv_user = (TextView) convertView.findViewById(R.id.id_tv_user);
            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Points points = pointsList.get(position);
        String time = points.getTime();
        String formateTime = time.substring(0,10)+" "+time.substring(11,13)+":"+time.substring(14,16)+":"+time.substring(17,19);
        String user = points.getRealName();
        String message = points.getMessage();
        viewHolder.tv_time.setText(formateTime);
        viewHolder.tv_user.setText(user);
        viewHolder.tv_message.setText(message == null ? "无" : message);
        String host = MyApplication.share().getResources().getString(R.string.basicIP);
        ImageLoader.getInstance().displayImage("http://" + host + "/mobilepoint/" + points.getFileName(), viewHolder.imageView,MyApplication.options);
        final int p = position;
        viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Points points1 = pointsList.get(p);
                Activity activity = UIHandler.uiHandler.getActivityByName(TakeInformationActivity.class.getSimpleName());
                Intent intent = new Intent(activity, ShowImage.class);
                intent.putExtra("points",points1);
                activity.startActivity(intent);

            }
        });
        convertView.setTag(R.id.id_points,points);
        return convertView;
    }

    public static class ViewHolder
    {
        public TextView tv_time;
        public TextView tv_user;
        public TextView tv_message;
        public ImageView imageView;
    }
}
